package 试题和试卷;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import 数据库和网络.myDatebase;


public class paper {
    String name,maker;
    int questionNumber,definition,selection,blank,subjective,time,remainTime;
    double averageDifficulty;

    public ArrayList<question> questionSet=new ArrayList<>();


    public void getQuestions(String classString,String paperName){
        myDatebase queryDatebaes=new myDatebase("papercollection");
        String sql=String.format("select * from papercollection.%s",paperName);
        try {
            queryDatebaes.statement.execute(sql);
            queryDatebaes.resultSet=queryDatebaes.statement.executeQuery(sql);
            while(queryDatebaes.resultSet.next()){
                String Text=queryDatebaes.resultSet.getString("questionText");
                String Title=queryDatebaes.resultSet.getString("questionName");
                String Type=queryDatebaes.resultSet.getString("questionPattern");
                int Difficulty=queryDatebaes.resultSet.getInt("questionDifficulties");
                int FullScore=queryDatebaes.resultSet.getInt("fullScore");
                String answer=queryDatebaes.resultSet.getString("answer");
                if(Type.equals("definition")){
                    question a=new definitionQuestion(Title,Text,Type,Difficulty,FullScore,answer);
                    questionSet.add(a);
                }else if(Type.equals("selection")){
                    question a=new selectionQuestion(Title,Text,Type,Difficulty,FullScore,answer);
                    myDatebase optionFinder=new myDatebase("questioncollection");
                    sql="select * from questioncollection.selection_question where questionTitle='"+Title+"'";
                    optionFinder.statement.execute(sql);
                    optionFinder.resultSet=optionFinder.statement.executeQuery(sql);
                    while(optionFinder.resultSet.next()){
                       a.setOption_a(optionFinder.resultSet.getString("option_a"));
                       a.setOption_b(optionFinder.resultSet.getString("option_b"));
                       a.setOption_c(optionFinder.resultSet.getString("option_c"));
                       a.setOption_d(optionFinder.resultSet.getString("option_d"));
                       System.out.println(optionFinder.resultSet.getString("option_a"));
                       System.out.println(queryDatebaes.resultSet.getString("questionName"));
                    }
                    questionSet.add(a);
                }else if(Type.equals("fillingBlank")){
                    question a=new fillingblankQuestion(Title,Text,Type,Difficulty,FullScore,answer);
                    questionSet.add(a);
                }else{
                    question a=new subjectiveQuestion(Title,Text,Type,Difficulty,FullScore,answer);
                    questionSet.add(a);
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }/*这里顺便操作了一下，paper对象自带一个question的arraylist，给了他一个方法让他get到所有题目信息*/

    public String getName(){
        return name;
    }

    public void printQuestions(){
        Iterator at=questionSet.iterator();
        while (at.hasNext()){
            question t=(question)at.next();
            System.out.println(t.getType()+"  "+t.getText()+"  "+t.getTitle()+"  "+t.getDifficulty()+"  "+t.getFullScore());
        }
    }

    public int getQuestionNumber(){
        questionNumber=questionSet.size();
        return questionNumber;
    }

    public String setName(String name){
        this.name=name;
        return name;
    }

    public int getDefinitionNumber(){
        int a=0;
        Iterator at=questionSet.iterator();
        while (at.hasNext()){
            question t=(question)at.next();
            if(t.getType().equals("definition")){/*这里必须使用.equals方法*/
                a++;
            }
        }
    return a;
    }

    public int getFillingBlankNumber(){
        int a=0;
        Iterator at=questionSet.iterator();
        while (at.hasNext()){
            question t=(question)at.next();
            if(t.getType().equals("fillingBlank")){
                a++;
            }
        }
        return a;
    }

    public int getSelectionNumber(){
        int a=0;
        Iterator at=questionSet.iterator();
        while (at.hasNext()){
            question t=(question)at.next();
            if(t.getType().equals("selection")){
                a++;
            }
        }
        return a;
    }

    public int getsubjectiveNumber(){
        int a=0;
        Iterator at=questionSet.iterator();
        while (at.hasNext()){
            question t=(question)at.next();
            if(t.getType().equals("subjective")){
                a++;
            }
        }
        return a;
    }
    void browseQusetion(){}

    void addQuestion(){}

    void deleteQuestion(){}

    void getAverageDifficulty(){}

    public int getTime(){
        return this.time;
    }

    public void setTime(int a){
        this.time=a;
    }

    public int getRemainTime(){
        return this.remainTime;
    }

    public void setRemainTime(int a){
        this.remainTime=a;
    }

}
